# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgname=nerdctl
pkgver=1.3.1
pkgrel=1
pkgdesc='Docker-compatible CLI for containerd'
arch=('loong64' 'x86_64')
url='https://github.com/containerd/nerdctl'
license=('Apache')
depends=('containerd')
makedepends=('git' 'go')
optdepends=(
  'buildkit: to build OCI images'
  'cni-plugins: to run OCI images'
  'rootlesskit: for rootless mode'
  'slirp4netns: for rootless mode'
)
options=('!lto')
_commit='b224b280ff3086516763c7335fc0e0997aca617a'
source=(
  "$pkgname::git+$url.git#commit=$_commit"
  'skip-failing-test.patch'
)
sha512sums=('SKIP'
            'c168de6147edcebf31df4b2e22dc830cad62b81780d10105aada8e17347523747d413c4398bec3840f079a37177b6f9c3ca87c8f0119f4f651f7e4c6462eb8b3')
b2sums=('SKIP'
        'c4dd2803d0fb6ef0067ee2ec3fb34f606618e157264e03f9b607629900b11a774f484c8cb320054f90911400bb436ca65f024bf5d63f39766e5b7f19fd8814e5')

pkgver() {
  cd "$pkgname"
  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # create directory for build output
  mkdir build

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download

  # skip failing unit test
  patch -p1 -i ../skip-failing-test.patch
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS} \
    -X github.com/containerd/nerdctl/pkg/version.Version=$pkgver \
    -X github.com/containerd/nerdctl/pkg/version.Revision=$_commit" \
    -o build \
    ./cmd/...

  # generate shell auto-completions
  for completion in bash fish zsh; do
    ./build/nerdctl completion "$completion" > "build/$completion-completions"
  done
}

check() {
  cd "$pkgname"

  go test -v ./pkg/...
}

package() {
  cd "$pkgname"

  # binary & rootless scripts
  install -vDm755 -t "$pkgdir/usr/bin" build/nerdctl extras/rootless/*.sh

  # shell completions
  install -vDm644 build/bash-completions "$pkgdir/usr/share/bash-completion/completions/$pkgname"
  install -vDm644 build/fish-completions "$pkgdir/usr/share/fish/vendor_completions.d/$pkgname.fish"
  install -vDm644 build/zsh-completions "$pkgdir/usr/share/zsh/site-functions/_$pkgname"
}
